//
//  StorageManager.swift
//  JuMingNet
//
//  Created by wangjiayu on 2020/2/18.
//  Copyright © 2020 wangjiayu. All rights reserved.
//

import UIKit

public class StorageManager: NSObject {
    public static let share:StorageManager = StorageManager()
}
// MARK: - 对外暴露API
public extension StorageManager {
    func save(value:Bool,key:String){
        UserDefaults.standard.setValue(value, forKey: key)
    }
    
    func save(value:Any?,key:String){
        guard let value = value else { return }
        UserDefaults.standard.setValue(value, forKey: key)
    }
    
    func save(value:String?,key:String){
        guard let value = value else { return }
        UserDefaults.standard.setValue(value, forKey: key)
    }
    
    func save(value:Bool,key:UIViewController) {
        let tempKey = NSStringFromClass(key.classForCoder)
        UserDefaults.standard.setValue(value, forKey: tempKey)
    }
    
    func remove(key:String) {
        UserDefaults.standard.removeObject(forKey: key)
    }
    
    func bool(forKey:String) -> Bool {
        return UserDefaults.standard.bool(forKey: forKey)
    }
    
    func object(forKey:String) -> Any? {
        return UserDefaults.standard.object(forKey: forKey)
    }
    
    func string(forKey:String) -> String? {
        return UserDefaults.standard.string(forKey: forKey)
    }
    
    func bool(forkey:UIViewController) -> Bool {
        let key = NSStringFromClass(forkey.classForCoder)
        return UserDefaults.standard.bool(forKey: key)
    }
    
    func value(forkey:UIViewController,islarge:Bool = false) -> CGFloat {
        let key = NSStringFromClass(forkey.classForCoder)
        let result = UserDefaults.standard.bool(forKey: key)
        if islarge{
            return result ? 0.0 : 75.0
        }
        return result ? 0.0 : 50.0
    }
    
    func delete(key:String) {
        UserDefaults.standard.removeObject(forKey: key)
    }
}
